const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    team_id: '', // 小组ID
    type: '', // 活动类型
    key_id: '', // 活动ID
    teamDetails: '', // 小组详情
    fontSetSize: 'small', // 用户设置的字体大小（'small'：小，'middle'：中，'large'：大）
  },

  /**
   * 获取小组详情
   */
  getTeamDetails () {
    let token = wx.getStorageSync('token')
    app.http({
      config: {
        url: '/api/User/team_info',
        data: {
          token,
          team_id: this.data.team_id
        },
        method: 'POST'
      },
      isAuth: true
    }).then(res => {
      console.log(res, 'teamDetailsRes');
      this.setData({
        teamDetails: res.data.data
      })
      app.setTitle(this.data.teamDetails.team_name)
    }).catch(err => {
      console.log(err, 'teamDetailsErr')
    })
  },

  /**
   * 退出小组
   */
  handleOutTeam (e) {
    let _this = this
    // 弹出弹窗询问用户是否确定退出班级
    wx.showModal({
      title: '提示',
      content: '是否确定退出班级？',
      success (res) {
        if (res.confirm) { // 用户点击了确认
          let token = wx.getStorageSync('token')
          app.http({
            config: {
              url: '/api/User/out_team_user',
              data: {
                token,
                team_id: _this.data.team_id
              },
              method: 'POST'
            },
            isAuth: true
          }).then(res => {
            console.log(res, 'outTeamRes')
            wx.showToast({
              icon: 'none',
              title: res.data.message
            })
            let timer = setTimeout(() => {
              wx.hideToast()
              clearTimeout(timer)
              wx.navigateBack({
                delta: 2,
              })
            }, 1500)
          }).catch(err => {
            console.log(err, 'outTeamErr')
            wx.showToast({
              icon: 'none',
              title: err.data.message
            })
            let timer1 = setTimeout(() => {
              wx.hideToast()
              clearTimeout(timer1)
            }, 2000)
          })
        }
      }
    })
  },

  /**
   * 设为打卡小组
   */
  handleSetPunchTeam(){
    let token = wx.getStorageSync('token')
    app.http({
      config: {
        url: '/api/User/edit_my_team_is_invisible',
        data: {
          token,
          team_id: this.data.team_id,
          key_id: this.data.key_id,
          type: this.data.type
        },
        method: 'POST'
      },
      isAuth: true
    }).then(res => {
      console.log(res, 'setPunchTeamRes')
      wx.showModal({
        title: '提示',
        content: res.data.message,
        showCancel: false,
      })
    }).catch(err => {
      console.log(err, 'setPunchTeamErr')
      wx.showModal({
        title: '提示',
        content: err.data.message,
        showCancel: false,
      })
    })
  },

  onLoad: function (options) {
    let team_id = options.team_id
    let type = options.team_type
    let key_id = options.key_id
    this.setData({
      team_id,
      type,
      key_id,
    })
    this.getTeamDetails()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let fontSetSize = wx.getStorageSync('fontSetSize') // 本地缓存中用户设置的字体大小
    if (fontSetSize) {
      this.setData({
        fontSetSize
      })
    }
  },

})